﻿using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevMy.Model;
using DevMy.PublicSetUp;
using MDIDemo.Model;
using MDIDemo.PublicClass;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_EarthReportInPut: Form_BaseAlone
    {
        public Form_EarthReportInPut(string FormText)
        {
            InitializeComponent();
            SetComponent(FormText);
        }
        private WaitDialogForm waitDialogForm;
        private Class_MySqlDataBase class_MySqlDataBase, class_MySqlDataBaseOld;
        private Class_LookUpEdit class_LookUpEdit;
        private string WhereString = null;
        private bool BeginSgin = false;
        private string Separation = null;

        private void ChangeDate(int Index)
        {
            DateTime dateTime = DateTime.Now.AddMonths(-1 * Index);
            this.dateEdit1.DateTime = dateTime.AddDays(-1 * dateTime.Day + 1);
            this.dateEdit2.DateTime = dateTime.AddMonths(1).AddDays(-1 * dateTime.Day);
        }
        private string GetSeparation(int Index)
        {
            DateTime dateTime = DateTime.Now.AddMonths(-1 * Index);
            return dateTime.ToString("yyyyMM");
        }
        protected void SetComponent(string FormText)
        {
            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBaseOld = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , "yantu"
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);
            LookUpEditColumnModel[] lookUpEditColumnModels = new LookUpEditColumnModel[2];
            lookUpEditColumnModels[0] = new LookUpEditColumnModel("equipmentName", "设备名称");
            lookUpEditColumnModels[1] = new LookUpEditColumnModel("id", "设备编号");
            DataTable dataTable = new DataTable();
            dataTable = class_MySqlDataBaseOld.GetDataTable(@"Select id
            , equipmentName
            From vou_equipment ORDER BY sortNo");
            class_LookUpEdit = new Class_LookUpEdit();
            class_LookUpEdit.SetLookUpEdit(this.lookUpEdit1, "equipmentName", "id", lookUpEditColumnModels, "设备名称");
            this.lookUpEdit1.Properties.DataSource = dataTable;
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                this.lookUpEdit1.ItemIndex = 0;
            }
            dataTable.Dispose();

            this.radioGroup1.Properties.Items.Clear();
            DateTime dateTime = DateTime.Now;
            for (int i = 0; i < 12; i++)
            {
                this.radioGroup1.Properties.Items.Add(
                    new DevExpress.XtraEditors.Controls.RadioGroupItem(null
                    , dateTime.AddMonths(-1 * i).ToString("yyyy-MM")));
            }
            this.radioGroup1.SelectedIndex = 0;
            Separation = GetSeparation(this.radioGroup1.SelectedIndex);
            ChangeDate(this.radioGroup1.SelectedIndex);

            base.SetComponent(FormText, "I1009");
            //base.SetViewNo();
            base.SetAdd("指定计算", "指定批次，重新计算。");
            //base.SetEdit("变参计算", "将指定参数改变后，重新计算。");
            //base.SetDelete("全部计算", "将未计算过的原始数据，全部计算。");
            base.SetViewNo();
            base.SetAddNo();
            base.SetEditNo();
            base.SetDeleteNo();
            base.SetExcelNo();
            base.SetWordNo();
            base.EnableOutButton(false);
            base.SetGridDataSource(null);
            base.GetGridPage().PageSize = 50;

            BeginSgin = true;
        }

        public override void ToExcel(SaveFileDialog saveFileDialog)
        {
        }
        /// <summary>
        /// 指定计算
        /// </summary>
        /// <returns></returns>
        public override int AddData()
        {
            //return base.AddData();
            Form_Car1 form_Car1 = new Form_Car1();
            form_Car1.SetWinType(1);
            form_Car1.id = null;
            form_Car1.Ini();
            form_Car1.Text = this.Text;
            if (form_Car1.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("指定批次已经计算过，无法重新计算！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            form_Car1.Dispose();

            return -1;
        }
        /// <summary>
        /// 变参计算
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override int EditData(object id)
        {
            //            DateTime dateTime = DateTime.Now;
            //          return SetCol("1600447372649", 11, dateTime, dateTime, true);
            Form_Car2 form_Car1 = new Form_Car2();
            form_Car1.SetWinType(1);
            form_Car1.id = null;
            form_Car1.Ini();
            form_Car1.Text = this.Text;
            if (form_Car1.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("没有找到未计算的原始数据，无法重新计算！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            form_Car1.Dispose();

            return -1;
        }
        /// <summary>
        /// 全部计算
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override int DeleteData(object id)
        {
            //            return base.DeleteData(id);
            MessageBox.Show("没有找到未计算过的原始数据！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return -1;
        }
        public override void ToWord(SaveFileDialog saveFileDialog)
        {
            //base.ToWord(saveFileDialog);
            MessageBox.Show("未检测到有效的WORD版本！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public override void Finder(int PageNo)
        {
            waitDialogForm = new WaitDialogForm("正在玩命加载中......", "温馨提示");

            WhereString = string.Format(@"WHERE createTime < '{1}'
                AND createTime > '{0}'"
                , this.dateEdit1.DateTime.AddDays(-1).ToString("yyyy-MM-dd")
                , this.dateEdit2.DateTime.AddDays(1).ToString("yyyy-MM-dd"));
            if (this.lookUpEdit1.ItemIndex > -1)
                WhereString = WhereString + string.Format(" AND equipmentId = {0}", this.lookUpEdit1.EditValue.ToString());
            DataSet dataSet = this.GetFinder(PageNo, base.GetGridPage().PageSize, WhereString);
            base.SetGridDataSource(dataSet.Tables[0]);
            base.GetGridPage().SetRowCount(int.Parse(dataSet.Tables[1].Rows[0][0].ToString()));
            waitDialogForm.Close();
        }
        private DataSet GetFinder(int PageNo, int PageSize, string Address)
        {
            #region
            string SelectSql = @"Select id,
calCount,
createTime,
dataTime,
batchId,
projectId,
equipmentId,
channelNo,
calType,
count,
data1,
data2,
data3,
data4,
data5,
data6,
data7,
data8,
data9,
data10,
data11,
data12,
data13,
data14,
data15,
data16,
data17,
data18,
data19,
data20,
data21,
data22,
data23,
data24,
data25,
data26,
data27,
data28";
            #endregion

            string FormSql = string.Format(@" From input_data_{0}", Separation);
            //string FormSql = @" From vou_inidata";
            string WhereSql = Address;
            string GroupSql = null;
            string HavingSql = null;
            string OrderSql = @" ORDER BY createTime desc";
            string TotalFieldName = "*";

            PageSqlModel pageSqlModel = class_MySqlDataBase.GetSql(SelectSql
            , FormSql
            , WhereSql
            , GroupSql
            , HavingSql
            , OrderSql
            , PageNo
            , PageSize
            , TotalFieldName);

            return class_MySqlDataBase.GetPageDataSet(pageSqlModel);
        }


        private void Form_EarthReport_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (waitDialogForm != null)
            {
                waitDialogForm.Dispose();
            }
            class_MySqlDataBase.CloseSqlConnection();
            class_MySqlDataBaseOld.CloseSqlConnection();
        }

        private void gridPage1_RowCellClickEventOut(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            GridView gridView = sender as GridView;
            if (e.Column.FieldName == "batchId")
            {
                string batchId = e.CellValue.ToString();
                if (batchId != null)
                {

                    Form_ResultCal form_ResultCal = new Form_ResultCal();
                    form_ResultCal.BatchId = batchId;
                    form_ResultCal.SetWinType(-1);
                    if (form_ResultCal.ShowDialog() == DialogResult.OK)
                    {

                    }
                    form_ResultCal.Close();
                    form_ResultCal.Dispose();
                }
            }
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (BeginSgin)
            {
                RadioGroup radioGroup = sender as RadioGroup;
                ChangeDate(radioGroup.SelectedIndex);
                Separation = GetSeparation(radioGroup.SelectedIndex);
            }
        }
    }
}
